package org.android10.viewgroupperformance.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.google.gson.Gson;
import org.android10.gintonic.Cache.CacheDatabase;
import org.android10.gintonic.Cache.CacheMemory;
import org.android10.gintonic.Cache.CacheUtil;
import org.android10.gintonic.annotation.DebugTrace;
import org.android10.viewgroupperformance.R;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.android.schedulers.AndroidSchedulers;

import java.util.Random;

public class MainActivity extends Activity {

    private Button btnRelativeLayoutTest;
    private Button btnLinearLayoutTest;
    private Button btnFrameLayoutTest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CacheUtil.addCacheHolder("default", DataHolder.INSTANCE);
        CacheUtil.setApplicationContext(this);
        setContentView(R.layout.activity_main);
        mapGUI();
    }

    /**
     * Maps Graphical User Interface
     */
    private void mapGUI() {
        this.btnRelativeLayoutTest = (Button) findViewById(R.id.btnRelativeLayout);
        this.btnRelativeLayoutTest.setOnClickListener(btnRelativeLayoutOnClickListener);

        this.btnLinearLayoutTest = (Button) findViewById(R.id.btnLinearLayout);
        this.btnLinearLayoutTest.setOnClickListener(btnLinearLayoutOnClickListener);

        this.btnFrameLayoutTest = (Button) findViewById(R.id.btnFrameLayout);
        this.btnFrameLayoutTest.setOnClickListener(btnFrameLayoutOnClickListener);
    }



    private class MyType {
        private String content;

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }
    /**
     * see this
     * 参数无要求
     * 返回值必须是Observable<T> 类型
     *
     * @param ccf 参数之一
     * @return 返回可观察对象
     * <p>
     * 注解参数
     * timeout: 超时时长(ms) 设置0为不超时
     * secondTimeout: 如果超时，允许在请求最新数据的过程中，返回的备用超时时长 设置0为不需要备用数据
     * <p>
     * example
     * @Cache(timeOut = 5000, secondTimeout = 10000)
     * 数据5秒超时，如果超时则允许在请求最新数据的过程中，返回10秒内的备用数据。
     * 被缓存命中时，本方法内所有代码不会运行 直接返回缓存的返回值
     *
     * 多个注解靠近方法的先执行
     */
    @DebugTrace(level = 2, enable = false)
    @CacheDatabase(clazz = MyType.class,timeOut = 10000, timeOutReturnIn = 20000)
    @CacheMemory(timeout =  5000)
    private Observable<MyType> getObData(String ccf) {
        StringBuilder strBuilder = new StringBuilder();
        for (int j = 0; j < 10; j++) {
            strBuilder.append(ccf);
        }
        final String tag = strBuilder.toString();
        Log.d(tag, "call in get ob data");

        return Observable.just(ccf).map(new Func1<String, MyType>() {
            @Override
            @DebugTrace(level = 2)
            public MyType call(String string) {
                StringBuilder builder = new StringBuilder();
                for (int j = 0; j < 10; j++) {
                    builder.append(string);
                }
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                MyType myType = new MyType();
                myType.setContent(builder.toString());
                return new Gson().fromJson(new Gson().toJson(myType), MyType.class);
            }
        });
    }

    private View.OnClickListener btnRelativeLayoutOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //openActivity(RelativeLayoutTestActivity.class);
            int i = new Random().nextInt(3);
            StringBuilder builder = new StringBuilder();
            for (int j = 0; j < 10; j++) {
                builder.append(i);
            }
            final String tag = builder.toString();
            Log.d(tag, "random = " + i);
            //String data = getData(i + "");
            //Log.d("bruce", "data = " + data);
            getObData(i + "").subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<MyType>() {
                        @Override
                        public void call(MyType s) {
//                String name = Thread.currentThread().getName();
//                Log.d(tag, "thread = " + name);
                            Log.d(tag, "ob data = " + s.getContent());
                        }
                    });
            errorCache(123);
        }
    };

    //@CacheMemory // error return clazz! annotation won't execute
    private int errorCache(int a) {
        return 1;
    }

    private View.OnClickListener btnLinearLayoutOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //openActivity(LinearLayoutTestActivity.class);

        }
    };

    private View.OnClickListener btnFrameLayoutOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openActivity(FrameLayoutTestActivity.class);
        }
    };

    /**
     * Open and activity
     */
    private void openActivity(Class activityToOpen) {
        Intent intent = new Intent(this, activityToOpen);
        startActivity(intent);
    }
}
